//
//  HomeCopyWritingViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/7/7.
//

#import "HomeCopyWritingViewController.h"
#import "CopyPopView.h"
#import <YCCardView/YCCardView.h>
#import "ZhenYanDetailsViewController.h"
#import "ZhenYanListViewController.h"
#import "RippleAnimationView.h"
#import "MottoModel.h"
@interface HomeCopyWritingViewController ()<YCCardViewDelegate,YCCardViewDataSource>
@property (nonatomic , strong) YCCardView *cardView;
@property (nonatomic , strong) RippleAnimationView * rippleView;
@property (nonatomic , strong) UIImageView * headerView;

@end

@implementation HomeCopyWritingViewController
- (void)SQ_viewWillAppear:(BOOL)animated{
    if (self.jsonArray.count<=0) {
        [self SQ_Header];
    }
    [self.headerView sd_setImageWithURL:SQURLImage([UserModel share].portrait)];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.clearColor;
    self.zx_hideBaseNavBar = YES;
    WS(weakSelf);
    SQCustomButton * rightBtn = [[SQCustomButton alloc]initType:SQCustomButtonRightImageType imageSize:CGSizeMake(10, 10) midmargin:10];
    rightBtn.text = SQNSLocalString(@"生成历史");
    rightBtn.backgroundColor = SQColor3DA59D;
    rightBtn.layer.cornerRadius = 14;
    rightBtn.font = FONT_REGULAR(12);
    rightBtn.textColor = UIColor.whiteColor;
    [self.view addSubview:rightBtn];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(30);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(28);
        make.right.equalTo(self.view.mas_right).offset(20);
    }];
    [rightBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        ZhenYanListViewController * vc = [[ZhenYanListViewController alloc]init];
               [weakSelf SQ_pushViewControllerFull:vc];
    }];
}

- (void)SQ_UIViewForSelfView{
    
    [self.view addSubview:self.cardView];
    [self.cardView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
    }];
    [self.view addSubview:self.rippleView];
    [self.rippleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(200);
        make.centerX.centerY.equalTo(self.view);
    }];
    [self.view addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.centerY.equalTo(self.rippleView);
                make.size.mas_equalTo(180);
    }];
}
- (void)SQ_Header{
    self.rippleView.hidden = NO;
    self.headerView.hidden = NO;
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"size"];
    [dic setObject:@"10" forKey:@"current"];
    [HttpTool post:@"motto/findList" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSArray * array = [MottoModel  mj_objectArrayWithKeyValuesArray:httpModel.data];
        if (array.count <=0) {
            [self SQ_Foother];
        } else {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.rippleView.hidden = YES;
                self.headerView.hidden = YES;
                [self.jsonArray removeAllObjects];
                [self.jsonArray addObjectsFromArray:array];
                [self.cardView yc_reloadDataWithAnimation:YES];
            });
        }
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
  
}
-(void)SQ_Foother{
    [HttpTool post:@"motto/generator" paramDic:[NSMutableDictionary new] successBlock:^(BaseModel * _Nonnull httpModel) {
        [self SQ_Header];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
}


/// 当前显示的卡片
/// @param cardView 父视图
/// @param cardCell 子视图
/// @param indexRow 当前索引
- (void)yc_cardDragView:(YCCardView *)cardView updateDisplayCell:(YCCardCell *)cardCell cellForRowAtIndex:(NSInteger)indexRow {
  
    CopyPopView *cell = (CopyPopView*)cardCell;
    cell.model = [self.jsonArray objectAtIndex:indexRow];
}

 
/// cell点击事件
/// @param cardView 父视图
/// @param indexRow 索引
- (void)yc_cardView:(YCCardView *)cardView didSelectedIndex:(NSUInteger)indexRow {
//    ZhenYanDetailsViewController * vc = [[ZhenYanDetailsViewController alloc]init];
//    MottoModel * model = [self.jsonArray objectAtIndex:indexRow];
//    vc.userId = model.userid;
//    [self SQ_pushViewControllerFull:vc];
}

/**
 卡片是否可以移动，拖拽的时候，可以进行一些条件控制，比如喜欢次数用完了，需要用户充值vip等
 * @param cardView 卡片拖拽视图
 * @param isRightDirection 是否为右边
 */
- (BOOL)yc_cardDragView:(YCCardView *)cardView cheackCardHasCanDragWithDragingDirection:(BOOL )isRightDirection {
  
    return true;
}
/**
 卡片拖拽完成代理
 * @param cardView 卡片拖拽视图
 * @param cardIndex 卡片索引位置
 * @param isRight 是否向右侧滑动
 */
- (void)yc_cardDragViewFinish:(YCCardView *)cardView cardCellIndex:(NSInteger )cardIndex direction:(BOOL )isRight{
    MottoModel * model = [self.jsonArray objectAtIndex:cardIndex];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:model.userid forKey:@"id"];
    if (isRight) {
        [dic setObject:@"0" forKey:@"status"];
    } else {
        [dic setObject:@"1" forKey:@"status"];
    }
    [HttpTool post:@"motto/sure" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        if (cardIndex == self.jsonArray.count -1) {
            [self SQ_Header];
        }
        } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
            [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];

}


#pragma mark - YCCardViewDataSource

- (NSInteger)yc_numberOfRowsInCardDragView:(YCCardView *)cardView {
    return self.jsonArray.count;
}

- (UIEdgeInsets)yc_edgeInsetsInCardDragView:(YCCardView *)cardView {
    return UIEdgeInsetsMake(20,15, 30, 15);
}

- (YCCardCell *)yc_creatCellForCardView:(YCCardView *)cardView {
    CopyPopView *cell =  [[CopyPopView alloc]init];
    WS(weakSelf);
    [cell.leftButton setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf.cardView yc_nextCardWithDirection:NO];
    }];
    [cell.loveBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf.cardView yc_nextCardWithDirection:YES];
    }];
    [cell.pingbiBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf.cardView yc_nextCardWithDirection:NO];
    }];
    return cell;
}


/**
 加载更多数据
 */
- (void)yc_loadDataMore {
    [HttpTool post:@"motto/generator" paramDic:[NSMutableDictionary new] successBlock:^(BaseModel * _Nonnull httpModel) {
       
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
}
 

- (YCCardView *)cardView{
    if (!_cardView) {
        _cardView = [[YCCardView alloc]initWithFrame:self.view.frame];
        _cardView.delegate = self;
        _cardView.dataSource = self;
    }
    return _cardView;
}

- (RippleAnimationView *)rippleView{
    if (!_rippleView) {
        _rippleView = [[RippleAnimationView alloc]initWithFrame:CGRectZero animationType:AnimationTypeWithBackground];
    }
    return _rippleView;
}
- (UIImageView *)headerView{
    if (!_headerView) {
        _headerView = [[UIImageView alloc]init];
        [_headerView sd_setImageWithURL:SQURLImage([UserModel share].portrait)];
        _headerView.layer.cornerRadius = 90;
        _headerView.layer.masksToBounds = YES;
        _headerView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _headerView;
}

@end
